#include <iostream>

using namespace std;

int numCollatzTerms(long long int start){
    long long int num = start;
    int terms = 1;
    while(num>1){
        if(num%2==0)
            num = num/2;
        else
            num = 3*num + 1;
        terms++;
        //cout << num << "\t| " <<terms << endl;
    }
    return terms;
}

int LongestCollatzSequence(int limit){
    int longest = 0;
    int num = 0;
    for( int i = 999999; i>0; i-- ){
        int colTerms = numCollatzTerms(i);
        cout << i << "\t|" << num << "\t|" << longest << endl;
        if( colTerms > longest){
            longest = colTerms;
            num = i;
        }
    }
    return num;
}

int main()
{
    cout << LongestCollatzSequence(1000000) << endl;

    return 0;
}
